package com.niit.chat.controller;

import com.niit.chat.service.MessageSessionService;
import com.niit.chat.vo.MessageSessionVO;
import com.niit.chat.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息会话控制器
 */
@RestController
@RequestMapping("/api/messages")
public class MessageSessionController {

    @Autowired
    private MessageSessionService messageSessionService;

    /**
     * 获取用户的所有会话列表
     * @param userId 用户ID
     * @return 会话列表
     */
    @GetMapping("/sessions/{userId}")
    public Result<List<MessageSessionVO>> getUserSessions(@PathVariable Long userId) {
        List<MessageSessionVO> sessions = messageSessionService.getUserSessions(userId);
        return Result.success(sessions);
    }

    /**
     * 获取两个用户之间的会话
     * @param userId 当前用户ID
     * @param friendId 好友用户ID
     * @return 会话信息
     */
    @GetMapping("/session/{userId}/{friendId}")
    public Result<MessageSessionVO> getSession(@PathVariable Long userId, @PathVariable Long friendId) {
        MessageSessionVO session = messageSessionService.getOrCreateSession(userId, friendId);
        return Result.success(session);
    }

    /**
     * 删除会话
     * @param sessionId 会话ID
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/session/{sessionId}")
    public Result<Boolean> deleteSession(@PathVariable Long sessionId, @RequestParam Long userId) {
        boolean result = messageSessionService.deleteSession(sessionId, userId);
        return Result.success(result);
    }
}